package com.taihua_tech.basic.modul2.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taihua_tech.basic.modul2.dao.QiuNingDao;
import com.taihua_tech.basic.modul2.service.QiuNingService;
import com.taihua_tech.basic.util.DataParam;
import com.taihua_tech.basic.util.DataRow;

@Controller
@RequestMapping("/modul2")
public class QiuNingController {
	@Autowired//注入
	private QiuNingDao qiuNingDao;
	
	@Autowired//注入
	private QiuNingService qiuNingService;
	
	@RequestMapping("/qiuNingList.html")
	public String initPage(ModelMap model,@RequestParam Map<String,String> map){
		System.out.println("进入了 QiuNingController 控制器方法");
		String msg=map.get("message");
		List<DataRow>list=qiuNingDao.findQiuNingList(new DataParam());
		System.out.println(list);
		model.addAttribute("renlist", list);
		model.addAttribute("message", msg);
		return "/modul2/QiuNingPage";
	}
	
	@PostMapping ("/findQiuNingList.do")
	@ResponseBody
	public  List<DataRow>  findQiuNingList(@RequestBody DataParam param){
		System.out.println("param333:"+param);
		int i=1/0;  //这里异常，aop会触发，记录日志
		List<DataRow>list=qiuNingDao.findQiuNingList(new DataParam());
		return list;
	}
	
	@PostMapping ("/getQiuNingRow.do")
	@ResponseBody
	public  DataRow  getQiuNingRow(@RequestBody DataParam param){
		DataRow row=qiuNingDao.getQiuNingRow(param);
		return row;
	}
	
	// get请求 要用 map 接收
	@GetMapping ("/delQiuNing.do" )
	@ResponseBody
	//直接返回字符串的写法
	public  String  delQiuNing(@RequestParam Map map){
		DataParam param=new DataParam();
		param.putAll(map);
		String msg="";
		int i=qiuNingDao.delQiuNing(param);
		msg=i>0?"sucess":"fail";
		return msg;
	}
	
	@PostMapping ("/insterQiuNing.do" )
	@ResponseBody
	public  String  insterQiuNing(@RequestBody DataParam param){
		String msg="";
		int i=qiuNingDao.insterQiuNing(param);
		msg=i>0?"sucess":"fail";
		return msg;
	}
	
	@PostMapping ("/updateQiuNing.do" )
	@ResponseBody
	public  String  updateQiuNing(@RequestBody DataParam param){
		String msg="";
		int i=qiuNingDao.updateQiuNing(param);
		msg=i>0?"sucess":"fail";
		return msg;
	}
	 
	 
	@PostMapping ("/shiwuTest.do" )
	@ResponseBody
	public  String  shiwuTest(@RequestBody DataParam param,Model model) throws Exception {
		return qiuNingService.shiwuTest(param);
	}
	
	// get请求 要用 map 接收
	@GetMapping ("/getget.do" )
	@ResponseBody
	public  String  getget(@RequestParam Map<String, Object> map) throws Exception {
		JSONObject jsonObj=new JSONObject(); 
		jsonObj.put("result","sucess" );
		jsonObj.put("message","转账成功！" );
		return jsonObj.toJSONString();
	}
}




